﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ProjectManageSystemApi.Models;
using ProjectManageSystemApi.Models.BuildingSiteBillModel;
using ProjectManageSystemApi.Services;
using ProjectManageSystemApi.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManageSystemApi.Controllers
{
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = Grouping.GroupName_v1)]
    public class BuildingSiteBillController : Controller
    {
        readonly IBuildingSiteBillServices _buildingSiteBillServices;
        readonly IUserServices _userServices;
        readonly Config _config;

        public BuildingSiteBillController(IBuildingSiteBillServices buildingSiteBillServices,
            IUserServices userServices, Config config)
        {
            _buildingSiteBillServices = buildingSiteBillServices;
            _userServices = userServices;
            _config = config;
        }

        [HttpPost, Route("Add")]
        public ApiResult Add([FromHeader(Name = "Authorization")] string auth, [FromBody] AddBuildingSiteBillDto dto)
        {
            if (dto == null)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            // 获取当前UserId
            if (!_userServices.GetUserIdByToken(auth.ReplaceBearer(), out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (_buildingSiteBillServices.ExistBillNoById(dto.BillNo))
            {
                return ApiResultStatus.Fail(ApiResultCodes.BILL_NO_EXIST);
            }
            bool r = _buildingSiteBillServices.AddBillAndImages(dto, userId);
            return ApiResultStatus.Create(r, ApiResultCodes.OPERATION_FAILED);
        }

        [HttpPost, Route("Upd")]
        public ApiResult Upd([FromBody] UpdBuildingSiteBillDto dto)
        {
            if (dto == null || dto.Id <= 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_buildingSiteBillServices.ExistId(dto.Id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (_buildingSiteBillServices.ExistBillNoById(dto.BillNo, dto.Id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.BILL_NO_EXIST);
            }
            bool r = _buildingSiteBillServices.UpdBillAndImages(dto);
            return ApiResultStatus.Create(r, ApiResultCodes.OPERATION_FAILED);
        }

        [HttpPost, Route("Get")]
        public ApiResult Get([FromBody] PageBo<BuildingSiteBillParams> pageBo)
        {
            if (pageBo == null || pageBo.Param == null
                || !pageBo.Param.ProjectId.HasValue || pageBo.Param.ProjectId.Value <= 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (pageBo.Size <= 0)
            {
                pageBo.Size = 10;
            }
            if (pageBo.Page <= 0)
            {
                pageBo.Page = 1;
            }
            return ApiResultStatus.Success(_buildingSiteBillServices.Get(pageBo));
        }

        [HttpPost, Route("GetMyBill")]
        public ApiResult GetMyBill([FromHeader(Name = "Authorization")] string auth,
            [FromBody] PageBo<BuildingSiteBillParams> pageBo)
        {
            if (pageBo == null || pageBo.Param == null
                || !pageBo.Param.ProjectId.HasValue || pageBo.Param.ProjectId.Value <= 0
                || pageBo.Param.StartTime <= _config.MinDate
                || pageBo.Param.EndTime <= _config.MinDate)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_userServices.GetUserIdByToken(auth.ReplaceBearer(), out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            pageBo.Param.CreateUser = userId;
            return ApiResultStatus.Success(_buildingSiteBillServices.Get(pageBo));
        }

        [HttpPost, Route("Del")]
        public ApiResult Del(uint id)
        {
            if (id < 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_buildingSiteBillServices.ExistId(id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            bool r = _buildingSiteBillServices.DelBill(id);
            return ApiResultStatus.Create(r, ApiResultCodes.OPERATION_FAILED);
        }

        /// <summary>
        /// PC页面上点击确认按钮
        /// </summary>
        /// <param name="auth"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("ConfirmBill")]
        public ApiResult ConfirmBill([FromHeader(Name = "Authorization")] string auth, uint id)
        {
            if (id <= 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_buildingSiteBillServices.ExistId(id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_userServices.GetUserIdByToken(auth.ReplaceBearer(), out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            bool r = _buildingSiteBillServices.ConfirmBillById(id, userId);
            return ApiResultStatus.Create(r, ApiResultCodes.OPERATION_FAILED);
        }

        /// <summary>
        /// PC页面上点击回退订单按钮，顺便写点备注
        /// </summary>
        /// <param name="auth"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost, Route("ReturnBack")]
        public ApiResult ReturnBack([FromHeader(Name = "Authorization")] string auth, [FromBody] BuildingSiteBillReturnDto data)
        {
            if (data == null || data.Id <= 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            if (!_buildingSiteBillServices.ExistId(data.Id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_userServices.GetUserIdByToken(auth.ReplaceBearer(), out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            bool r = _buildingSiteBillServices.ReturnBack(data, userId);
            return ApiResultStatus.Create(r, ApiResultCodes.OPERATION_FAILED);
        }

        /// <summary>
        /// 手机端显示回退订单列表
        /// </summary>
        /// <param name="pageBo"></param>
        /// <returns></returns>
        [HttpPost, Route("GetReturnBack")]
        public ApiResult GetReturnBack([FromHeader(Name = "Authorization")] string auth, PageBo<BuildingSiteBillReturnBackParams> pageBo)
        {
            if (pageBo == null)
            {
                pageBo = new PageBo<BuildingSiteBillReturnBackParams>();
            }
            if (pageBo.Size <= 0)
            {
                pageBo.Size = 10;
            }
            if (pageBo.Page <= 0)
            {
                pageBo.Page = 1;
            }
            if (pageBo.Param == null)
            {
                pageBo.Param = new BuildingSiteBillReturnBackParams();
            }
            if (!_userServices.GetUserIdByToken(auth.ReplaceBearer(), out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            return ApiResultStatus.Success(_buildingSiteBillServices.GetReturnBack(pageBo, userId));
        }

        /// <summary>
        /// 手机端重置回退的订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("ResetReturnBackById")]
        public ApiResult ResetReturnBackById(ResetBuildingSiteBill data)
        {
            if (data == null || data.Id <= 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_buildingSiteBillServices.ExistId(data.Id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            bool b = _buildingSiteBillServices.ResetReturnBackById(data);
            return ApiResultStatus.Create(b, ApiResultCodes.OPERATION_FAILED);
        }

    }
}
